import java.util.Random;

import javax.media.opengl.GL;


public class TriangleCut extends PuzzleCut{
	double size;

	public TriangleCut()
	{
		Random rand = new Random();
		//between PuzzlePiece.size/10
		//and PuzzlePiece.size*.3
		double lower = PuzzlePiece.size/3;
		double upper = PuzzlePiece.size*.6;
		size = rand.nextDouble()*(upper - lower)+lower;
		//randomize invert
		double inv = rand.nextDouble();
		inverted = true;
		if (inv>=.5)
			inverted = false;
	}
	public TriangleCut(double size, boolean inverted)
	{
		this.size = size;
		this.inverted = inverted;
	}
	/**
	 * the color of the piece and the background color have to be specified,
	 * the piece will know which one to use by the way it was setup.
	 * @param gl
	 * @param color
	 * @param backColor
	 * @param Coordinate location of cut
	 * @param Coordinate location of piece
	 */
	public void display(GL gl,PuzzleColor color,PuzzleColor backColor,Coordinate coord,Coordinate location,int direction)
	{
		Coordinate x,y,z; //coordinate locations from the
		x = new Coordinate(coord.x,coord.y,0);//initializes all of them to the center point
		y = new Coordinate(coord.x,coord.y,0);
		z = new Coordinate(coord.x,coord.y,0);
		if(!inverted)
		{
			switch(direction){
			case 0:x.x+=(size/2);y.x-=(size/2);z.y+=size;break;
			case 1:x.x-=(size/2);y.x+=(size/2);z.y-=size;break;
			case 2:x.y-=(size/2);y.y+=(size/2);z.x-=size;break;
			case 3:x.y+=(size/2);y.y-=(size/2);z.x+=size;break;
			}
			gl.glColor3f(color.getRed(), color.getGreen(), color.getBlue());
		}
		else
		{
			//only the z changes in this one from the inverted ones
			switch(direction){
			case 0:x.x+=(size/2);y.x-=(size/2);z.y-=size;break;
			case 1:x.x-=(size/2);y.x+=(size/2);z.y+=size;break;
			case 2:x.y-=(size/2);y.y+=(size/2);z.x+=size;break;
			case 3:x.y+=(size/2);y.y-=(size/2);z.x-=size;break;
			}
			gl.glColor3f(backColor.getRed(), backColor.getGreen(), backColor.getBlue());
		}
		gl.glBegin (GL.GL_POLYGON);
		gl.glVertex2f(x.getX(),x.getY());
		gl.glVertex2f(y.getX(),y.getY());
		gl.glVertex2f(z.getX(),z.getY());
		gl.glEnd();
	}
	/**
	 * a clone method that clones the same shape in a different spot
	 * the position is determined by coord coordinate
	 * @param invert
	 * @return
	 */
	public PuzzleCut clone(boolean bool)
	{
		PuzzleCut result =new TriangleCut(size,inverted);
		if(bool)
				result.invert();
		return result;
	}
}
